package com.nuotu.datawall.clouddata.control;

import android.widget.TextView;


import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class DateControl {

    private static DateControl dateControl;
    private SimpleDateFormat sdf = null;
    private Timer serviceTimer = null;
    private TextView textView;

    public static DateControl getInstance() {
        if (dateControl == null) {
            dateControl = new DateControl();
        }
        return dateControl;
    }

    public DateControl init(final TextView textView) {
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (serviceTimer == null) {
            serviceTimer = new Timer();
        }
        this.textView=textView;
        return dateControl;
    }
    public void toStart(){
        if (serviceTimer != null) {
            serviceTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    //发送广播
                    if (textView != null) {
                        x.task().autoPost(new Runnable() {
                            @Override
                            public void run() {
                                textView.setText(getCurrTime());
                            }
                        });
                    }
                }
            }, 1000, 1000);
        }
    }
    public void toStop(){
        if(serviceTimer!=null){
            serviceTimer.cancel();
            serviceTimer=null;
        }
    }

    /**
     * 获取最新系统时间
     *
     * @return
     */
    private String getCurrTime() {
        return sdf.format(new Date());
    }

}
